package org.ricks.net.action.context.head;

import java.nio.ByteBuffer;

public class RpcHeadMeta extends DefInnerHeadMeta {
    private final int requestId;

    public RpcHeadMeta(short cmd, boolean isResponse, boolean isHeartbeat, int requestId) {
        super((byte) (FLAG_RPC |
                        (isResponse ? FLAG_RESPONSE : 0) |
                        (isHeartbeat ? FLAG_HEARTBEAT : 0)),
                cmd);
        this.requestId = requestId;
    }

    @Override
    public int rpcRequestId() {
        return requestId;
    }

    @Override
    public ByteBuffer generateHeader(ByteBuffer buffer) {
        if (buffer.capacity() < 7) {
            buffer = ByteBuffer.allocate(7);  // 修复：检查缓冲区大小
        }
        buffer.clear();
        buffer.put(flags())
                .putShort(cmd)
                .putInt(requestId);
        buffer.flip();
        return buffer;
    }
}
